package org.hyh.autoconfigure.core.configuration;

import org.apache.ibatis.session.SqlSessionFactory;
import org.hyh.autoconfigure.core.interceptor.SqlPrintInterceptor;
import org.hyh.core.log.SqlLogInterceptor;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Properties;

/**
 * @author :yihui.He
 * @description :
 * @date :2021/2/18
 */
@Configuration
@ConditionalOnBean(SqlSessionFactory.class)
@AutoConfigureAfter(MybatisAutoConfiguration.class)
public class SqlLogConfiguration {

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;

    @PostConstruct
    public void addSqlLogInterceptor() {
        // SqlLogInterceptor sqlLogInterceptor = new SqlLogInterceptor();
        SqlPrintInterceptor sqlPrintInterceptor =new SqlPrintInterceptor();
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(sqlPrintInterceptor);
        }
    }

}
